
//////////////////////// Page Initializing ////////////
//
jQuery(document).ready(function() {			
	
	
	$('.nav_bg a').click(function() {
		$('.nav_bg a').removeClass('active');
		$(this).addClass('active');
	});								
});


//////////////////////// LOADING A PAGE ////////////
//

function loadPage(sourceDiv, actionURL, serviceId) {	
		
	$.ajax ({		
		url:	  	actionURL + "?set_ajax_view",
		data:		"service_id=" + serviceId,		
		dataType: 	"html",
		beforeSend: function() {					
			$('div#serviceListContent').hide();	
			$('div#pageContent').hide("slow");
			$('div#' + sourceDiv).hide("slow");			
		},
		complete: 	function() {
			$('div#' + sourceDiv).show("fast");			
		},
		success:  function(data) {
			$("#" + sourceDiv).html(data);			
		}
	});		
}

////////////// LIST SERVICE by DEPARTMENT ///////////////////
//

function listService() {
	$('#current_dept').text($('#departmentList :selected').attr('label'));	
	$.ajax ({		
		url:	  	"ListServiceAjax?set_ajax_view",
		data:		"serviceGroupId=" + $('#serviceGroupList').val() +
					"&disabled=" + $('#disabled').val(),
		dataType: 	"json",
		beforeSend: function() {	
			$('div#search_button').hide();
			$('div#search_button').next().show();												
		},
		complete: 	function() {						
			$('div#search_button').next().hide();
			$('div#search_button').show();
		},
		success:  function(data) {
			serviceTable = $('#service_list');
			fillTableData(serviceTable, data.body);
			fillTotalTableData($('#service_container'), data.total);	
			
			if ($('tbody tr', serviceTable).length > 0) {
				$("#pagination").show();
				serviceTable.trigger("update");																			
				serviceTable.tablesorterPager({
					container: $("#pagination"),
					size: 10,										
					positionFixed: false
				});
			} else {
				$("#pagination").hide();
			}				
								
			$('#service_container').show();		
		}
	});	
}

////////////// FILL DATA and STYLE SERVICE TABLE ///////////////////
//

function fillTotalTableData(parentDiv, data) {
	$('table.total_table', parentDiv).remove();
	parentDiv.append(data);
	parentDiv.children('table:last-child').addClass('total_table');
}

function fillTableData(table, data) {
	$('tbody', table).children().remove();	
	$('tbody', table).append(data);			
}

////////////// LOCK AND UNLOCK A SERVICE ///////////////////
//
function lockService(serviceId) {
	var message = _('Are you sure to temporary lock') + ' <span class="impression">' 
		+ $('#service_list tr#' + serviceId + ' td:nth-child(2)').text() + '</span> service?'
		+ '<p>'
		+ _('<span class="impression">Notice:</span> When registering a new ticket, receptionist will not see this service')
		+ '</p>';  
	show(message, serviceId, 'lock');
}

function unlockService(serviceId) {
	var message = _('Unlock service') + ' <span class="impression">' 
		+ $('#service_list tr#' + serviceId + ' td:nth-child(2)').text() + '</span> ?';
	 show(message, serviceId, 'unlock');
}

function show(message, serviceId, control) {
	promptConfirm('',message,function() {
		submitChangeServiceStatus(serviceId, control);
		return true;
	},'OkCancel','',_('Cancel'));
}

function submitChangeServiceStatus(serviceId, control) {
	$.ajax ({	
		url:	  	"ChangeServiceStatusAjax?set_ajax_view",
		data:		"service_id=" + serviceId,		
		beforeSend: function() {				
			$('div#search_button').hide();					
			$('div#barLoading').show();			
		},
		complete: 	function() {			
			$('div#barLoading').hide();
			$('div#search_button').show();
		},
		success:  function(data) {
			serviceTable = $('#service_list');
			var html = '';
			var status = '';
			if (control == 'lock') {
				status = '<span class="impression">' + _("Temporary lock") + '</span>';
				html = '<a class="btn" href="javascript:unlockService(' + serviceId + ');" title="' + _("Unlock service") + '">' 
				+ '<img src="../images/unlock_small.png" alt="' + _("Unlock") + '"></img></a>'; 				
			} else if (control == 'unlock') {
				status = '<span>' + _("In use") + '</span>';
				html = '<a class="btn" href="javascript:lockService(' + serviceId + ');" title="' + _("Temporary lock service") + '">' 
				+ '<img src="../images/lock_small.png" alt="' + _("Temporary lock") + '"></img></a>'; 				
			}
			$('tr#'+serviceId + ' td:nth-child(8)', serviceTable).html(status);
			$('tr#'+serviceId + ' a.btn', serviceTable).parent().html(html);
											
		}
	});	
}

////////////////// ADD-REMOVE ROOMS /////////////////

var roomArray = new Array();
function addRoom() {
	var roomId = $('#room :selected').val();
	var roomName = $('#room :selected').attr('label');
		
	if ($.inArray(roomId, roomArray) >= 0) {
		generalDialog(null,null,_('Existed <span class="impression">{0}</span>',roomName),'warn');
		
		return;
	}	
	
	var html =
		'<tr id="' + roomId + '"><td><input type="hidden" value="' + roomId + '" name="roomId[]">'
			+ ' <span>' + roomName + '</span>	</td>'
			+ '<td> <a href="javascript:removeRoom(' + roomId + ');" title="' + _("Delete room") + '">' 
			 		+ '<img src="../images/remove_small.png" alt="' + _("Delete") + '"></img>' 
			+ '</a></td></tr>';
	$('tbody', $('#room_list')).append(html);
	roomArray.push(roomId);
			
	$('#room_list tr').removeClass('alt');
	$('#room_list tr:even').addClass('alt');
	$('#room_list').show();
		
}

function removeRoom(roomId) {
	$('#room_list tr#' + roomId).nextAll().toggleClass('alt');
	$('#room_list tr#' + roomId).remove();
	roomArray.pop(roomId);
	if ($('#room_list tbody tr').length == 0) {
		$('#room_list').hide('medium'); 
	} 
}


/////////////////// SAVE SERVICE ////////////
//
function saveService() {
	var res = isReadyToSubmit();
	if (res == false) {
		return ;
	}
	var submitData = $('#add_service_form').formSerialize();
	var serviceId = $('#service_id').val(); 
	$.ajax ({
		type:   "POST",
		url:	"checkDuplicate?set_ajax_view",
		data:	submitData,
		success:	function(data) {
			if(data == 0){
				submitSave();
			}
			if (data == 1){
				$('#status_message').text(_('Duplicated service code')).show();
				$('#code').css("border-color", "red");
			} 
			if (data == 2) {
				$('#status_message').text(_('Duplicated service name')).show();
				$('#service_name').css("border-color", "red");
			}
		}
	});
}

function submitSave(){
	var submitData = $('#add_service_form').formSerialize();
	$.ajax ({
		type:   "POST",
		url:	"SaveServiceAjax?set_ajax_view",
		data:	submitData,
		beforeSend: function() {
			$('#btn_save').hide();
			$('#barLoading').show();				
		},
		complete: 	function() {
			$('#btn_save').show();
			$('#barLoading').hide();
		},
		success:	function(data) {
			$('div#pageContent').hide('slow');
			listService();
			$('div#serviceListContent').show();
		}
	});
}
function editService(serviceId) {
	$('div#serviceListContent').hide();
	loadPage('pageContent', 'Edit', serviceId);
}

function isReadyToSubmit() {
	if (!isNotEmpty($('#code'), _("Empty code"), "status_message")) {
		return false;
	}		
	
	if (!isNotEmpty($('#service_name'), _("Empty service name"), "status_message")) {
		return false;
	}		
	
	if (!isNotEmpty($('#price'), _("Empty service price"), "status_message")) {
		return false;
	}
	
	if (!checkNumber($('#price'), _("Invalid service price"), "status_message", "#AAA")) {
		return false;
	}
	
	if (!checkNumber($('#insurance_price'), _("Invalid service price"), "status_message", "#AAA")) {
		return false;
	} 
	
	return true;
}


